package com.ts.simplemusic;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.imageview.ShapeableImageView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PlayFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PlayFragment extends Fragment {

    private static final String TAG = "PlayFragment";

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View mView;

    private ImageView mPlayingControl;
    private TextView mMusicName;
    private ShapeableImageView mMusicBlum;
    private TextView mMusicSinger;
    private TextView mPlayingMusicCurrentTime;
    private SeekBar mPlayingSeekbar;
    private TextView mPlayingMusicDuration;
    private ImageView mPlayingBefore;
    private ImageView mPlayingNext;

    public PlayFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PlayFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PlayFragment newInstance(String param1, String param2) {
        PlayFragment fragment = new PlayFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_play, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mView = view;
        initView();
        initListener();
        initData();
    }

    private void initData() {
        Music currentMusic = MusicHelper.getInstance().getCurrentMusic();
        Log.d(TAG, "initData: currentMusic = " + currentMusic);
        if (currentMusic != null) {
            // 这里暂时只更新歌曲名称
            mMusicName.setText(currentMusic.getTitle());
        }
    }

    private void initView() {
        mMusicBlum = mView.findViewById(R.id.music_blum);
        mMusicName = mView.findViewById(R.id.music_name);
        mMusicSinger = mView.findViewById(R.id.music_singer);
        mPlayingMusicCurrentTime = mView.findViewById(R.id.playing_music_current_time);
        mPlayingSeekbar = mView.findViewById(R.id.playing_seekbar);
        mPlayingMusicDuration = mView.findViewById(R.id.playing_music_duration);
        mPlayingBefore = mView.findViewById(R.id.playing_before);
        mPlayingControl = mView.findViewById(R.id.playing_or_pause);
        mPlayingNext = mView.findViewById(R.id.playing_next);
    }

    private void initListener() {
        mPlayingControl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换播放状态
                mPlayingControl.setSelected(!mPlayingControl.isSelected());
            }
        });
        mPlayingBefore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int prev = MusicHelper.getInstance().prev();
                Log.d(TAG, "onClick: prev = " + prev);
                if (prev != 0) {
                    Toast.makeText(requireContext(), "当前已经是第一首了", Toast.LENGTH_SHORT).show();
                } else {
                    // 更新歌曲信息
                    updateMusic();
                }
            }
        });
        mPlayingNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int next = MusicHelper.getInstance().next();
                Log.d(TAG, "onClick: next = " + next);
                if (next != 0) {
                    Toast.makeText(requireContext(), "当前已经是最后一首了", Toast.LENGTH_SHORT).show();
                } else {
                    // 更新歌曲信息
                    updateMusic();
                }
            }
        });
        mPlayingSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void updateMusic() {
        Music currentMusic = MusicHelper.getInstance().getCurrentMusic();
        mMusicName.setText(currentMusic.getTitle());
    }
}